#include<bits/stdc++.h>
using namespace std;

int n,m;
string str,ans;
vector<int> edges[26];
int in[30],in1[30];
bool visit[30];

int main(){
	cin>>n>>m;
	memset(in,0,sizeof(in));
	memset(visit,false,sizeof(visit));
	int sumn=0;
	int i;
	for(i=1;i<=m;i++){
		cin>>str;
		int x=str[0]-'A',y=str[2]-'A';
		if(!visit[x]){
			visit[x]=true;
			sumn++;
		}
		if(!visit[y]){
			visit[y]=true;
			sumn++;
		}
		in[y]++;
		edges[x].emplace_back(y);
		//Tuopu sort
		queue<int> que;
		for(int k=0;k<n;k++){
			in1[k]=in[k];
			if(visit[k]&&in[k]==0) {
				que.push(k);
			}
		}
		ans="";
		int tag=1;
		while(!que.empty()){
			int size=que.size();
			if(size>1){
				tag=-1;
			}
			for(int j=0;j<size;j++){
				int x=que.front();
				que.pop();
				ans+=(char)(x+'A');
				for(auto k:edges[x]){
					in1[k]--;
					if(in1[k]==0){
						que.push(k);
					}
				}
			}
		};
		if(tag!=-1&&ans.size()==n){
			cout<<"Sorted sequence determined after ";
			cout<<i;
			cout<<" relations: "+ans+'.';
			return 0;
		}
		else if(ans.size()<sumn){
			cout<<"Inconsistency found after ";
			cout<<i;
			cout<<" relations.";
			return 0;		
		}
	}
	cout<<"Sorted sequence cannot be determined.";
	return 0;
}
